package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.MessageCheckIn;

public class MessageCheckInLogic extends AbstractBL{
	public List<MessageCheckIn> GenerateMessageCheckInRequest(long hotelId)
	{
		List<MessageCheckIn> list = new ArrayList<MessageCheckIn>();
		
		MessageCheckIn messagefolio;
		if(GetMessageCheckInTitle(1,"Thank!") == null){
			messagefolio = new MessageCheckIn();
			messagefolio.setHotelId(hotelId);
			messagefolio.setMessage("Thank!");
			messagefolio.setThxmessage("Thank you very much.");
			list.add(messagefolio);
		}
		
		return list;
	}
	
	public MessageCheckIn GetMessageCheckInTitle(long hotelId,String title)
	{
		Query q = new Query(EntityName.MessageCheckIn);
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterStatus = new FilterPredicate("status",FilterOperator.EQUAL,true);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterTitle = new FilterPredicate("message",FilterOperator.EQUAL,title);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus, filterAvaliable,filterTitle);
		
		q.setFilter(filter);
		
		return GetMessage(datastore.prepare(q).asSingleEntity());
	}
	
	public MessageCheckIn GetMessage(Entity entity)
	{
		if(entity == null) return null;
		MessageCheckIn item = new MessageCheckIn();
		item.setEntity(entity);
		return item;
	}
	
	public MessageCheckIn AddMessageCheckIn(long hotelId,String txtMessage,String thxMessage,String cardbody,boolean ispublish) throws EntityNotFoundException{
		MessageCheckIn b = new MessageCheckIn();
		b.setHotelId(hotelId);
		b.setMessage(txtMessage);
		b.setThxmessage(thxMessage);
		b.setPublishonweb(ispublish);
		b.setCardbody(cardbody);
		
		Key k = datastore.put(b.getEntity());
		b.setId(k.getId());
		b.setKey(k);

		return b;
	}
	
	public MessageCheckIn EditMessageCheckIn(long id,String txtMessage,String thxMessage,String cardbody,boolean ispublish) throws EntityNotFoundException{
		MessageCheckIn b =  GetMessageCheckIn(id);
		b.setMessage(txtMessage);
		b.setThxmessage(thxMessage);
		b.setPublishonweb(ispublish);
		b.setCardbody(cardbody);
		
		Key k = datastore.put(b.getEntity());
		b.setId(k.getId());
		b.setKey(k);

		return b;
	}
	
	public MessageCheckIn GetMessageCheckIn(long id)
	{
		Key key = KeyFactory.createKey(EntityName.MessageCheckIn, id);
		try {
			Entity lang = datastore.get(key);
			MessageCheckIn c = new MessageCheckIn();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public List<MessageCheckIn> GetMessageCheckInAll(long hotelId) {
		Query q = new Query(EntityName.MessageCheckIn);
		Filter filterHotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		q.setFilter(filterHotel);

		return GetList(datastore.prepare(q).asList(
				FetchOptions.Builder.withDefaults()));
	}
	
	public List<MessageCheckIn> GetList(List<Entity> entities) {
		List<MessageCheckIn> list = new ArrayList<MessageCheckIn>();
		if(entities!=null)
		for (Entity e : entities) {
			MessageCheckIn a = new MessageCheckIn();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}
}
